<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <!-- 导入样式表 -->
    <link rel="stylesheet" href="./assets/news.css" />
  </head>

  <body>
    <!-- 新闻列表 -->
    <div id="news-list">
      </div>
    </div>

    <!-- 导入axios -->
    <script src="./lib/axios.js"></script>
    <script>
      // 等页面加载之后发送请求
      // window ==> load 
      // document => DOMContentLoaded
      // window.onload = function(){}

      window.addEventListener('load', () => {

        const box = document.querySelector('#news-list')

        // 1. 调用axios.get发送请求
        const api = 'http://ajax-api.itheima.net/api/news'
        // ==> 接口  ==》

        axios.get(api).then(res => {
          // res response ，如果请求成功，后端返给我的数据
          console.log(res)
          const htmlArr = res.data.data.map(el => {
            // 对象的解构
            const {cmtcount, id, img, source, time, title} = el
            return `
            <div class="news-item">
            <img class="thumb" src="${img}" alt="" />
            <div class="right-box">
              <!-- 新闻标题 -->
              <h1 class="title">${title}</h1>
              <div class="footer">
                <div>
                  <!-- 新闻来源 -->
                  <span>${source}</span>&nbsp;&nbsp;
                  <!-- 发布日期 -->
                  <span>${time}</span>
                </div>
                <!-- 评论数量 -->
                <span>评论数：${cmtcount}</span>
              </div>
            </div>
          </div>
            `
          })

          box.innerHTML = htmlArr.join('')
          
        }) // end then


      })
    </script>
  </body>
</html>
